

const db=wx.cloud.database()
Page({
  data:{
   swiperList:[],
   visible:false,
   extraction:false,
   value:'',
   wxcode:'',
   fileList:[],
   newArrList:[],
   b:false,
   List:[],
   block:false,
  
  },
 onLoad(options){


  },
  onShow(){
 this.getSwiper()
  },
 async getSwiper(){
   const {data:res}=await db.collection('banner_s').get()
   this.setData({
     swiperList:res[0].banner_pic
   })
   console.log(res);
  },
  handlePopup(e){
    console.log(e);
    let b=JSON.parse(e.currentTarget.dataset.b)
    this.setData({
      visible:true,
      b:b
    })
},
change(e){
this.setData({
  value:e.detail.value
})
},
onVisibleChange(e){
 this.setData({
  visible:e.detail.visible
 })

},
VisibleChange(){
  this.setData({
    extraction:true
   })
},



handleSuccess(e) {
  let newArr=[]
  const { files } = e.detail
  this.setData({
    fileList: files
  });
  
this.data.fileList.forEach(item => {
    wx.cloud.uploadFile({
    cloudPath: item.name,
    filePath: item.url, // 文件路径
  }).then(res => {
    // get resource ID
    newArr.push(res.fileID)
    this.setData({
      newArrList:newArr
    })
    console.log(res);

  })
})


},
handleRemove(e) {
  const { index } = e.detail;
  const { fileList } = this.data;
  fileList.splice(index, 1);
  this.setData({
    fileList,
  });
},
getwxcode(e){
  this.setData({
    wxcode:e.detail.value
  })

},
submit(){

  const {newArrList,wxcode,value,b}=this.data
 
  if(newArrList===[] || wxcode==='' || value==='' ){
    wx.showToast({
      icon:'none',
      title: '请填写信息!',
    })

  }else{
  
   db.collection(b ? 'boy_paper' : 'girl_paper').add({
      data:{
      wx_code:wxcode,
      picture:newArrList,
      introduce:value
   }
 }).then(()=>{
   wx.showToast({
     title: '放入成功!',
   })
   this.setData({
    visible:false,
    wxcode:'',
    fileList:[],
    value:''
   })
 })
    

  }


},
 exPaper(){
   this.setData({
  extraction:true
  })
 db.collection( 'boy_paper' )
  .aggregate()
  .sample({
    size: 1
  })
  .end().then(res=>{
    this.setData({
    List:res.list[0]
    })
    console.log(res);
  })
  
},
exPaperG(){
  this.setData({
    extraction:true
    })
  db.collection( 'girl_paper' )
  .aggregate()
  .sample({
    size: 1
  })
  .end().then(res=>{
    this.setData({
    List:res.list[0]
    })
    console.log(res);
  })
},
onClose(){
  this.setData({
    extraction:false ,
    List:[] 
  })
},
imgchange(){
  this.setData({
    block:true,
    extraction:false  
  })
},
Close(e){
  console.log(e);
  const {trigger}=e.detail
  if(trigger==='overlay'){
    this.setData({
      block:false
    })
  }

},

})

